<?php
/**
 ** jgirldb project, apr 2010
 **  open-source project using adodb, xajax, smarty for managing advanced contact information
 **   eg. dating girls, just friends, online contacts, english lesson students
 **  supporting english/japanese natively in utf8
 **
 **/
session_start();
ini_set("include_path", "/home/mike/jgirldb");
require_once 'config.php';

$xajax->setRequestURI("index.php");

$xajax->debugOn();

// ajax function for drop-down menu to select action to perform
function onSelectCategory($data) {
    global $smarty;
    $objResponse = new xajaxResponse();
    $action = $data['actiontype'];
    switch($action) {
        case 'create':
            lookup_refer();
            $objResponse->addAssign("BBBB", "innerHTML", $smarty->fetch('create.tpl'));
            $objResponse->addAssign("AAAA", "innerHTML", '&nbsp;');
        break;
        case 'search':
            // do nothing just yet
            // $objResponse->addAssign("divsearch", "innerHTML", $smarty->fetch('query_name.tpl'));
            // $objResponse->addAssign("divlistresult", "innerHTML", '&nbsp;');
        break;
    }
    return $objResponse->getXML();
}

// ajax display results for a search performed
// currently just supporting searches by name
function showResult($data) {
    global $smarty;
    $objResponse = new xajaxResponse();
    $searchtype = $data['searchtype'];
    switch($searchtype) {
        case 'byname':
            $searchresults =  searchname($data['jgirlname']);
            $smarty->assign('searchresults', $searchresults);
            $objResponse->addAssign("divlistresult", "innerHTML", $smarty->fetch('searchresults.tpl'));
        break;
    }
    return $objResponse->getXML();
}

// brings up for for inputting values from which to create a new jgirl, as it clears other divs
function showCreate() {
    global $smarty;
    init_lookups();
    $objResponse = new xajaxResponse();
    $objResponse->addAssign("divcreate", "innerHTML", $smarty->fetch('create/index.tpl'));
    $objResponse->addAssign("divstart", "innerHTML", '&nbsp;');
    $objResponse->addAssign("divsearch", "innerHTML", '&nbsp;');
    $objResponse->addAssign("divbutton", "innerHTML", '&nbsp;');
    $objResponse->addAssign("divlistresult", "innerHTML", '&nbsp;');
    return $objResponse->getXML();
}

// ajax to display a full jgirl, with all the child tables values
// needs lots of work
function showJgirl($jgirlid) {
    global $smarty;
    $objResponse = new xajaxResponse();
/** need to change target div for each one so they dont override each other when getxml is called **/
    $jgirlcore =  gettable_core($jgirlid);
    $smarty->assign('jgirlcore', $jgirlcore);
    $jgirlsellingpoint =  getlink_lookups($jgirlid, 'sellingpoint');
    $smarty->assign('jgirlsellingpoint', $jgirlsellingpoint);
    $jgirlcompatibility =  getlink_lookups($jgirlid, 'compatibility');
    $smarty->assign('jgirlcompatibility', $jgirlcompatibility);
    $jgirlhobby =  getlink_lookups($jgirlid, 'hobby');
    $smarty->assign('jgirlhobby', $jgirlhobby);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/core.tpl'));
    $jgirlprofiles =  gettable_gen($jgirlid, 'profiles');
    $smarty->assign('jgirlprofiles', $jgirlprofiles);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/profile.tpl'));
    $jgirlnanpas =  gettable_gen($jgirlid, 'nanpas');
    $smarty->assign('jgirlnanpas', $jgirlnanpas);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/nanpa.tpl'));
    $jgirlalias =  gettable_gen($jgirlid, 'alias');
    $smarty->assign('jgirlalias', $jgirlalias);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/alias.tpl'));
    $jgirlemailaddresses =  gettable_gen($jgirlid, 'emailaddresses');
    $smarty->assign('jgirlemailaddresses', $jgirlemailaddresses);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/emailaddress.tpl'));
    $jgirlphonenumbers =  gettable_gen($jgirlid, 'phonenumbers');
    $smarty->assign('jgirlphonenumbers', $jgirlphonenumbers);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/phonenumber.tpl'));
    $jgirllocations =  gettable_gen($jgirlid, 'locations');
    $smarty->assign('jgirllocations', $jgirllocations);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/location.tpl'));
    $jgirldates =  gettable_gen($jgirlid, 'dates');
    $smarty->assign('jgirldates', $jgirldates);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/date.tpl'));
    $jgirloverseas =  gettable_gen($jgirlid, 'overseas');
    $smarty->assign('jgirloverseas', $jgirloverseas);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/overseas.tpl'));
    $jgirlmeasurement =  gettable_gen($jgirlid, 'measurements');
    $smarty->assign('jgirlmeasurement', $jgirlmeasurement);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/measurements.tpl'));
    $jgirlemails =  gettable_gen($jgirlid, 'emails');
    $smarty->assign('jgirlemails', $jgirlemails);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/email.tpl'));
    $jgirlphotos =  gettable_gen($jgirlid, 'photos');
    $smarty->assign('jgirlphotos', $jgirlphotos);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/photo.tpl'));
    $jgirlcomments =  gettable_gen($jgirlid, 'comments');
    $smarty->assign('jgirlcomments', $jgirlcomments);
    $objResponse->addAssign("divviewjgirl", "innerHTML", $smarty->fetch('viewjgirl/comment.tpl'));

    return $objResponse->getXML();
}

$xajax->registerFunction("onSelectCategory");
$xajax->registerFunction("showResult");
$xajax->registerFunction("showCreate");
$xajax->registerFunction("showJgirl");
$xajax->processRequests();
$smarty->assign('xajax_js', $xajax->getJavascript());

$smarty->display('index.tpl');

?>
